﻿using PSI.Helper;
using PSI.Models.TMS;
using PSI.Services;
using PSI.Services.TMS;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PSI.Controllers {
    public class OutConsignationController : PSIController {
        public ActionResult Index() {
            var ps = new PermissionService();
            if (!ps.HasPermission(FIdConst.OUT_CONSIGNATION)) {
                return Redirect("/");
            }

            ViewBag.UserName = UserService.GetLoginUserName();

            ViewBag.UserName = UserService.GetLoginUserName();
            ViewBag.PermissionEdit = ps.HasPermission("3003-1") ? "1" : "0";
            ViewBag.PermissionConfirm = ps.HasPermission("3003-2") ? "1" : "0";

            return View();
        }

        [HttpPost]
        public ActionResult GetOutConsignationListData() {
            var data = new OutConsignationService().GetOutConsignationListData(this.Request.Form);
            return Json(data);
        }

        [HttpPost]
        public ActionResult GetOutConsignationDetailListData(string id) {
            var data = new OutConsignationService().GetOutConsignationDetailListData(id);
            return Json(data);
        }

        [HttpPost]
        public ActionResult EditOutConsignation() {
            var result = new OutConsignationService().EditOutConsignation(this.Request.Form);

            return Json(result);
        }

        [HttpPost]
        public ActionResult DeleteOutConsignation(string id) {
            var result = new OutConsignationService().DeleteOutConsignation(id);

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetOutConsignation(string id) {
            var data = new OutConsignationService().GetOutConsignation(id);

            return Json(data);
        }

        [HttpPost]
        public ActionResult EditOutConsignationDetail() {
            var result = new OutConsignationService().EditOutConsignationDetail(this.Request.Form);

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetOutConsignationDetail(string id) {
            var data = new OutConsignationService().GetOutConsignationDetail(id);

            return Json(data);
        }

        [HttpPost]
        public ActionResult DeleteOutConsignationDetail(string id, string parentId) {
            var result = new OutConsignationService().DeleteOutConsignationDetail(id, parentId);

            return Json(result);
        }

        [HttpPost]
        public ActionResult MakePlan(string id, string actionDate, string isweixin, string dispatcher)
        {
            var result = new OutConsignationService().MakePlan(id, actionDate, isweixin, dispatcher);

            return Json(result);
        }

        [HttpPost]
        public ActionResult CancelPlan(string id) {
            var result = new OutConsignationService().CancelPlan(id);

            return Json(result);
        }

        [HttpPost]
        public ActionResult Upload() {
            try {
                var file = this.Request.Files["file"];
                if (!file.FileName.EndsWith(".xls")) {
                    return Content("{success: false, msg: '请选择Excel文件'}");
                }

                var path = this.Server.MapPath("~");
                path += @"\upload";
                if (!Directory.Exists(path)) {
                    Directory.CreateDirectory(path);
                }

                var fileName = path + @"\" + IdGeneratorService.NewId() + "_" + file.FileName;

                file.SaveAs(fileName);

                var result = new OutConsignationService().Upload(fileName);

                return Content(result);
            } catch (Exception ex) {
                return Content("{success: false, msg: '" + ex.Message + "'}");
            }
        }

        public ActionResult ExportResView()
        {
            ViewBag.UserName = UserService.GetLoginUserName();
            return View();
        }

        [HttpPost]
        public ActionResult ExportResViewQuery()
        {
            var cc =(NameValueCollection) this.Request.Params;

            var result = new OutConsignationService().GetOutConsignationDetailListData("").Where(a => a.isweixin == IsWXStatus.release || a.isweixin == IsWXStatus.complete);
            if (!string.IsNullOrEmpty(cc["adr"].ToString()))
            {
                result=result.Where(a => a.Address==cc["adr"]);
            }

            var dt = cc["date"];
            if (dt.Contains("今天"))
            {
                result = result.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd"));
            }
            else if (dt.Contains("明天"))
            {
                result = result.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"));
            } else if (dt.Contains("昨天")) {
                result = result.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"));
            }

            var tempobj = new { Data = result.ToList<OutConsignationDetail>() };

            return Json(tempobj);
        }

        [HttpPost]
        public ActionResult ExportDetailRes(string date)
        {
            var result = new OutConsignationService().GetOutConsignationDetailListData("").Where(a => a.isweixin == IsWXStatus.release||a.isweixin==IsWXStatus.complete).ToList<OutConsignationDetail>();

            if (date.Contains("今天"))
            {
                result = result.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd")).ToList<OutConsignationDetail>();
            }
            else if(date.Contains("明天"))
            {
                result = result.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")).ToList<OutConsignationDetail>();

            } else if (date.Contains("昨天")) {

                result = result.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")).ToList<OutConsignationDetail>();
            }
            var data = new { Data = result };

            return Json(data);
        }

        [HttpPost]
        public ActionResult GetResListbyOrderID(string orderID)
        {
            var result = new WinXinResServer().GetexportByorder(orderID);
            var cc = new { data = result };
            return Json(cc);  
        }

        [HttpPost]
        public ActionResult ResDecision(string id)
        {
            var Exportdemil = new WinXinResServer().GetSinglebyID(id, 2);

            var Exportdateil = new OutConsignationService().GetOutConsignationDetail(Exportdemil.orderID.ToString());
            if (Exportdateil.Status != "待派车" && Exportdateil.Status != "待下达计划") return Json("当前计划已经被安排车辆！");


            var bol = new WinXinResServer().ExportResDecision(id);
            if (bol)
                return Json("");
            else
                return Json("数据问题，审核通过失败！");
        }

        public ActionResult ReportDetile()
        {
            ViewBag.UserName = UserService.GetLoginUserName();

            var time = Request.QueryString["time"].ToString();
            var add = Request.QueryString["address"].ToString();

            if (time == "本月小计")
            {
                time = DateTime.Now.ToString("yyyy-MM");
            }
            if (add == "本月合计")
            {
                add = "";
            }

            ViewBag.time = time;
            ViewBag.address = add;

            return View();
        }

    }
}
